<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\finance\controller;

use plugin\saiadmin\basic\BaseController;
use app\finance\logic\financeLogic;
use app\finance\validate\financeValidate;
use support\Request;
use support\Response;

/**
 * 金融列表控制器
 */
class financeController extends BaseController
{
    /**
     * 数据表主键
     */
    protected $pk = 'finance_id';
    
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new financeLogic();
        $this->validate = new financeValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['finance_category', ''],
            ['finance_institution', ''],
            ['start_time', ''],
            ['end_time', ''],
            ['is_display', ''],
            ['finance_title', ''],
            ['lang_code', ''],
        ]);
        $query = $this->logic->search($where) ->order('sort_order desc,create_time desc');
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    public function saves(Request $request) : Response
    {
        $data = $request->post();
//print_r($data);die;
        if (!$this->validate->scene('save')->check($data)) {
            return $this->fail($this->validate->getError());
        }

        foreach ($data['product_tags'] as $k =>$val){
            if($val == "" || empty($val)){
                unset($data['product_tags'][$k]);
            }
        }
        $data['product_tags'] =json_encode(array_values($data['product_tags']));
        $result = $this->logic->save($data);
        if ($result) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 修改数据
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function updates(Request $request, $id) : Response
    {
        $data = $request->post();
        if (!$this->validate->scene('update')->check($data)) {
            return $this->fail($this->validate->getError());
        }

        foreach ($data['product_tags'] as $k =>$val){
            if($val == "" || empty($val)){
                unset($data['product_tags'][$k]);
            }
        }
        $data['product_tags'] =json_encode(array_values($data['product_tags']));
        $result = $this->logic->where($this->pk, $id)->update($data);
        if ($result) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    public function  reads(Request $request) : Response
    {
        $id = $request->input('id', '');
        $result = $this->logic->where($this->pk, $id)->find()->toArray();
        $result['product_tags']=json_decode($result['product_tags']);

        if ($result) {
            // $this->afterChange('changeStatus');
            return $this->success($result);
        } else {
            return $this->fail('操作失败');
        }
    }

    public function changeStatus(Request $request) : Response
    {
        $id = $request->input('id', '');
        $status = $request->input('status', 1);
        $type = $request->input('type', 1);
        if($type == 1){
            $datas['is_display'] =$status;
        }
        $result = $this->logic->where($this->pk, $id)->update($datas);

        if ($result) {
            // $this->afterChange('changeStatus');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    public function updateSortOrder(Request $request) : Response
    {
        $id = $request->input('id', '');
        $sort_order = $request->input('sort_order', 1);

        $result = $this->logic->where($this->pk, $id)->update(['sort_order' => $sort_order,'update_time'=>date('Y-m-d H:i:s')]);

        if ($result) {
            $this->afterChange('changeStatus');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
     public function export(Request $request) : Response
    {
       
        $where = $request->more([
            ['finance_category', ''],
            ['finance_institution', ''],
            ['start_time', ''],
            ['end_time', ''],
            ['is_display', ''],
            ['finance_title', ''],
            ['lang_code', ''],
        ]);
        return $this->logic->export($where);
    }
    
}
